package com.digital.smartgrow.controller.businesscenter;

import com.digital.smartgrow.common.advice.Result;
import com.digital.smartgrow.dto.businesscenter.NewsPageDTO;
import com.digital.smartgrow.entity.businesscenter.News;
import com.digital.smartgrow.service.businesscenter.NewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 资讯管理 前端控制器
 * </p>
 *
 * @author xiewb
 * @since 2022-04-16
 */
@Api(tags = "资讯管理")
@RestController
@RequestMapping("${context.prefix}/news")
public class NewsController {

    @Autowired
    private NewsService newsService;

    @ApiOperation(value = "列表分页查询")
    @PostMapping("/getListByPage")
    public Object getListByPage(@RequestBody NewsPageDTO newsPageDTO){
        return newsService.getListByPage(newsPageDTO);
    }

    @ApiOperation(value = "资讯管理-详情")
    @GetMapping("/getInfo")
    public Object getInfo(Integer id){
        if (id == null) return Result.fail("id不能为空");
        return newsService.getInfo(id);
    }

    @ApiOperation(value = "资讯管理-删除")
    @GetMapping("/removeById")
    public Object removeById(Integer id){
        if (id == null) return Result.fail("id不能为空");
        return newsService.removeById(id);
    }

    @ApiOperation(value = "资讯管理-新增")
    @PostMapping("/add")
    public Object add(@Valid @RequestBody News news){
        return newsService.save(news);
    }

    @ApiOperation(value = "资讯管理-编辑")
    @PostMapping("/edit")
    public Object edit(@Valid @RequestBody News news){
        if (news == null || news.getId() == null) return Result.fail("id不能为空");
        return newsService.updateById(news);
    }
}
